package gervosou;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JSeparator;

public class VueNouvCompteSociete extends Vue {
	private JTextField textFieldNomSoc;
	private JTextField textFieldSolde;
	
	private JButton btnRechercher;
	private JButton btnAnnuler;
	private JButton btnAnnuler2;
	private JButton btnEnregistrer;
	
	private Societe _soc ;
	
	private JTextField textFieldAdresse;
	private JTextField textFieldNbc;
	private JTextField textFieldNumero;
	
	private JTextArea textAreaInfosComptes;

	private JPanel panel;
	
	private JLabel lblNomSociete;
	private JLabel lblComptesSociete;
	private JLabel lblSolde;
	private JLabel lblAdresse;
	private JLabel lblNombreDeComptes;
	private JLabel lblNumeroDuCompte;
	
	private JScrollPane scrollPane;
	
	private JSeparator separator;
	
	
	public VueNouvCompteSociete(Controleur c) {
		super(c);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 500, 500);
		panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		textFieldNomSoc = new JTextField();
		textFieldNomSoc.setBounds(249, 12, 114, 19);
		panel.add(textFieldNomSoc);
		textFieldNomSoc.setColumns(10);
		
		lblNomSociete = new JLabel("Nom Société :");
		lblNomSociete.setBounds(125, 14, 85, 15);
		panel.add(lblNomSociete);
		
		btnRechercher = new JButton("Rechercher");
		btnRechercher.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String nom = textFieldNomSoc.getText();
				// liaison de la vue avec l'objet observe
				 setSociete(getControleur().rechSociete(nom));
			}
			});
		btnRechercher.setBounds(86, 53, 107, 25);
		panel.add(btnRechercher);
		
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					getControleur().fermerVue(VueNouvCompteSociete.this);}
		});
		btnAnnuler.setBounds(259, 53, 107, 25);
		panel.add(btnAnnuler);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(12, 184, 418, 64);
		panel.add(scrollPane);
		
		textAreaInfosComptes = new JTextArea();
		textAreaInfosComptes.setEditable(false);
		scrollPane.setColumnHeaderView(textAreaInfosComptes);
		
		lblComptesSociete = new JLabel("Comptes Société :");
		lblComptesSociete.setBounds(12, 157, 119, 15);
		panel.add(lblComptesSociete);
		
		lblSolde = new JLabel("Solde :");
		lblSolde.setBounds(168, 260, 41, 15);
		panel.add(lblSolde);
		
		textFieldSolde = new JTextField();
		textFieldSolde.setBounds(249, 258, 114, 19);
		panel.add(textFieldSolde);
		textFieldSolde.setColumns(10);
		
		btnAnnuler2 = new JButton("Annuler");
		btnAnnuler2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueNouvCompteSociete.this);}
	});
		btnAnnuler2.setBounds(249, 337, 107, 25);
		panel.add(btnAnnuler2);
		
		btnEnregistrer = new JButton("Enregistrer");
		btnEnregistrer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			String s = textFieldSolde.getText();
			getControleur().nouvCompteSociete(s, _soc);
		}
		});
		
		btnEnregistrer.setBounds(86, 337, 107, 25);
		panel.add(btnEnregistrer);
		
		textFieldAdresse = new JTextField();
		textFieldAdresse.setEditable(false);
		textFieldAdresse.setBounds(79, 126, 114, 19);
		panel.add(textFieldAdresse);
		textFieldAdresse.setColumns(10);
		
		textFieldNbc = new JTextField();
		textFieldNbc.setEditable(false);
		textFieldNbc.setBounds(259, 126, 114, 19);
		panel.add(textFieldNbc);
		textFieldNbc.setColumns(10);
		
		lblAdresse = new JLabel("Adresse :");
		lblAdresse.setBounds(79, 109, 61, 15);
		panel.add(lblAdresse);
		
		lblNombreDeComptes = new JLabel("Nombre de comptes :");
		lblNombreDeComptes.setBounds(261, 109, 132, 15);
		panel.add(lblNombreDeComptes);
		
		lblNumeroDuCompte = new JLabel("Numero du compte :");
		lblNumeroDuCompte.setBounds(86, 297, 132, 15);
		panel.add(lblNumeroDuCompte);
		
		textFieldNumero = new JTextField();
		textFieldNumero.setEditable(false);
		textFieldNumero.setBounds(249, 295, 114, 19);
		panel.add(textFieldNumero);
		textFieldNumero.setColumns(10);
		
		separator = new JSeparator();
		separator.setBounds(86, 90, 277, 2);
		panel.add(separator);
}
	
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			textFieldAdresse.setEnabled(false);
			textFieldSolde.setEnabled(false);
			textFieldNumero.setEnabled(false);
			//btnEnregistrer.setEnabled(false);
			//btnRechercher.setEnabled(true);
			//btnAnnuler.setEnabled(true);
			//btnAnnuler2.setEnabled(false);
			break;
			}
		case finale: {
			//btnRechercher.setEnabled(false);
			textFieldAdresse.setEditable(false);
			textFieldSolde.setEnabled(true);
			textFieldNumero.setEditable(false);
//			btnAnnuler.setEnabled(false);
//			btnEnregistrer.setEnabled(true);
//			btnAnnuler2.setEnabled(true);
			break;
			}
		}
	}
	
	
	
	private Societe getSociete() {
		return _soc;
	}
	
	private void setSociete(Societe soc) {
		 _soc = soc;
	}
	
	public void alimente(Societe soc) {
		textFieldAdresse.setText(soc.getAdresse());
		textFieldNbc.setText(String.valueOf(soc.getNbComptes()));
		textAreaInfosComptes.setText("");
		for (CompteSociete cs : soc.getComptes()) {
			  textAreaInfosComptes.append( "numero " + String.valueOf(cs.getNumCompte()) + " solde " + String.valueOf(cs.getSolde()) + "\n");
		}
	}
	
	public void alimenteNum(int n) {
		textFieldNumero.setText(String.valueOf(n));
	}
}
